package com.cardvalue.sys.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cardvalue.sys.R;
import com.cardvalue.sys.entity.SelHasLeaseContract;

import java.util.ArrayList;

/**
 * 选择只有id  和titile的
 * Created by cardvalue on 2016/4/25.
 */
public class LinkRecylerAdapter  extends RecyclerView.Adapter{
    private ArrayList<SelHasLeaseContract> mlist;
    private Context context;
    private LayoutInflater inflater;
    public LinkRecylerAdapter(Context context,ArrayList<SelHasLeaseContract> mlist){
        this.context=context;
        this.mlist=mlist;
        this.inflater=LayoutInflater.from(context);

    }

    /**
     * 设置view,只做布局解析
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //View view=View.inflate(context,R.layout.list_textview,null);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_textview, parent, false );
        return new ViewHolder(view);
    }


    /**
     * 设置数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
   if(holder instanceof ViewHolder){
      ViewHolder viewHolder=(ViewHolder)holder;
       SelHasLeaseContract selHasLeaseContract=mlist.get(position);
       viewHolder.titile.setText(selHasLeaseContract.getTitle());
   }
    }

    /**
     * 设置adapter的条数
     * @return
     */
    @Override
    public int getItemCount() {
        return mlist!=null?mlist.size():0;
    }


    /**
     * viewHolder
     */
    class ViewHolder extends  RecyclerView.ViewHolder{
        TextView titile;
        //构造函数
        public ViewHolder(final View itemView){
            super(itemView);
            //初始化方法
            inint(itemView);
            //点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListenter!=null){
                        onItemClickListenter.OnItemClick(itemView,getLayoutPosition());
                    }
                }
            });
        }
        public void inint(View view){
            titile=(TextView)view.findViewById(R.id.title);

        }
    }


    /**
     * 对外暴露的点击事件接口
     */

    public interface  OnItemClickListenter{
         void  OnItemClick(View v,int position);
    }


    public OnItemClickListenter onItemClickListenter;


    public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter){
        this.onItemClickListenter=onItemClickListenter;
    }
}
